$(function(){
    var morease ={};

    /**
     * 轮播图自适应
     */
    morease.resize = function(){
        var imgHeight = 0;
        var wtmp = $("body").width();
        $(".slide-wrap ul li").each(function(){
            $(this).css({width:wtmp + "px"});
        });
        //设置视频的高度与图片保持一直
        $(".sliderimg").each(function(){
            $(this).css({width:wtmp + "px"});
            imgHeight = $(this).height();
        });
    };
    /**
     * 初始化获取首页轮播图信息
     * **/
    morease.initBanner = function(){
        $.ajax({
            async: false,
            url: '/banner/findBannerIndexList',
            success: function (result) {
                if (result && result.code == 200) {
                    var slideWarp = $('.slide-wrap');
                    var slideWidth = slideWarp.outerWidth();//获取轮播图宽度
                    var scale = (slideWidth/1920).toFixed(2);
                    var slideHeight = 774*scale;
                    var html = '<ul>';
                    $.each(result.data,function(index,banner){
                        if( index <4 ){//最多取四张
                            if(banner.bannerUrl.indexOf("image") != -1 ){
                                html +='<li class="slide home-slide">';
                                html +='<a href="'+banner.bannerLink+'"><img src="'+banner.bannerUrl+'" alt="" class="sliderimg" width="'+slideWidth+'" height="'+slideHeight+'"></li></a>';
                            }else{
                                html +='<li class="slide home-slide">';
                                html +='<a href="'+banner.bannerLink+'"><video src="'+banner.bannerUrl+'" class="sliderimg" controls="controls" width="'+slideWidth+'" height="'+slideHeight+'"></li></a>';
                            }

                        }
                    })
                    html +='</ul>';
                    slideWarp.empty().append(html);

                    //获取图片高度
                    //morease.resize();

                    $('.slide-wrap').unslider({
                        dots: true,
                        fluid: true
                    });

                    $('.slide-wrap').css({"height":slideHeight});
                }
            },
            error: function () {
                layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
            }
        });
    }

    /**
     * 初始化获取首页轮播图信息
     * **/
    /*morease.initProduct = function(){
        $.ajax({
            async: false,
            url: '/product/findRecommendProduct',
            success: function (result) {
                if (result && result.code == 200) {
                    var $content = $('#content');
                    $.each(result.data,function(index,product){
                        var $product = $content.find('.product:eq('+index+')');
                        if( $product != undefined ){
                            //设置产品名称
                            $product.find('.product-name').text(product.productName);
                            $product.find('.product-name').attr("title",product.productName);
                            //设置产品名称
                            $product.find('img').attr("src", product.covenPhoto);
                            $product.find('img').attr("title", product.productName);
                            //设置产品详情连接
                            $product.find('a').attr("href","/web/productDetails?productId="+product.productId);
                        }else{
                            return false;
                        }
                    })
                }
            },
            error: function () {
                layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
            }
        });
    }*/

    /**
     * 初始化获取首页轮播图广告栏信息
     * **/
    morease.initAdvert = function(){
        $.ajax({
            async: false,
            url: '/banner/findBannerAdvertList',
            success: function (result) {
                if (result && result.code == 200) {
                    $.each(result.data,function(index,banner){
                        var bannerPosition = banner.bannerPosition;
                        switch ( bannerPosition ){
                            case 51:
                                $(".advert1 img").attr("src",banner.bannerUrl);
                                $(".advert1 a").attr("href",banner.bannerLink);
                                break;
                            case 52:
                                $(".advert2 img").attr("src",banner.bannerUrl);
                                $(".advert2 a").attr("href",banner.bannerLink);
                                break;
                            case 53:
                                $(".advert3 img").attr("src",banner.bannerUrl);
                                $(".advert3 a").attr("href",banner.bannerLink);
                                break;
                            case 54:
                                $(".advert4 img").attr("src",banner.bannerUrl);
                                $(".advert4 a").attr("href",banner.bannerLink);
                                break;
                            case 55:
                                $(".advert5 img").attr("src",banner.bannerUrl);
                                $(".advert5 a").attr("href",banner.bannerLink);
                                break;
                            case 56:
                                $(".advert6 img").attr("src",banner.bannerUrl);
                                $(".advert6 a").attr("href",banner.bannerLink);
                                break;
                        }
                    })
                }
            },
            error: function () {
                layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
            }
        });
    }

    morease.initBanner();
    morease.initAdvert();
    /*morease.initProduct();*/
    header.selLiCss("index");
})
